package ar.org.sstuyv

import grails.converters.JSON

import org.springframework.dao.DataIntegrityViolationException

class IIUUOrdenamientoTerritorialController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [IIUUOrdenamientoTerritorialInstanceList: IIUUOrdenamientoTerritorial.list(params), IIUUOrdenamientoTerritorialInstanceTotal: IIUUOrdenamientoTerritorial.count()]
    }

//    def create() {
//        [IIUUOrdenamientoTerritorialInstance: new IIUUOrdenamientoTerritorial(params)]
//    }
//
//	def edit(Long id) {
//		def IIUUOrdenamientoTerritorialInstance = IIUUOrdenamientoTerritorial.get(id)
//		if (!IIUUOrdenamientoTerritorialInstance) {
//			flash.message = message(code: 'default.not.found.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), id])
//			redirect(action: "list")
//			return
//		}
//
//		[IIUUOrdenamientoTerritorialInstance: IIUUOrdenamientoTerritorialInstance]
//	}
	
	def form(Long id) {
		def IIUUOrdenamientoTerritorialInstance
		if (id){
			IIUUOrdenamientoTerritorialInstance = IIUUOrdenamientoTerritorial.get(id)
		} else {
			IIUUOrdenamientoTerritorialInstance = new IIUUOrdenamientoTerritorial(params)
		}
		[IIUUOrdenamientoTerritorialInstance: IIUUOrdenamientoTerritorialInstance]
	}	
	
	
    def save() {
        def IIUUOrdenamientoTerritorialInstance = new IIUUOrdenamientoTerritorial(params)
        if (!IIUUOrdenamientoTerritorialInstance.save(flush: true)) {
            render(view: "create", model: [IIUUOrdenamientoTerritorialInstance: IIUUOrdenamientoTerritorialInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), IIUUOrdenamientoTerritorialInstance.id])
        redirect(action: "show", id: IIUUOrdenamientoTerritorialInstance.id)
    }

    def show(Long id) {
        def IIUUOrdenamientoTerritorialInstance = IIUUOrdenamientoTerritorial.get(id)
        if (!IIUUOrdenamientoTerritorialInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), id])
            redirect(action: "list")
            return
        }

        [IIUUOrdenamientoTerritorialInstance: IIUUOrdenamientoTerritorialInstance]
    }

    

	def update() {
		 
//		def ordenamiento = IIUUOrdenamientoTerritorial.get(params.id)
		
		def ordenamiento = IIUUOrdenamientoTerritorial.findById(params.id)
		ordenamiento.properties = params
		
		ordenamiento.save()
		
		def jsonData = [rs: 'edit', id: ordenamiento.id]
		render jsonData as JSON

	}
	
//    def update(Long id, Long version) {
//        def IIUUOrdenamientoTerritorialInstance = IIUUOrdenamientoTerritorial.get(id)
//        if (!IIUUOrdenamientoTerritorialInstance) {
//            flash.message = message(code: 'default.not.found.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), id])
//            redirect(action: "list")
//            return
//        }
//
//        if (version != null) {
//            if (IIUUOrdenamientoTerritorialInstance.version > version) {
//                IIUUOrdenamientoTerritorialInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
//                          [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial')] as Object[],
//                          "Another user has updated this IIUUOrdenamientoTerritorial while you were editing")
//                render(view: "edit", model: [IIUUOrdenamientoTerritorialInstance: IIUUOrdenamientoTerritorialInstance])
//                return
//            }
//        }
//
//        IIUUOrdenamientoTerritorialInstance.properties = params
//
//        if (!IIUUOrdenamientoTerritorialInstance.save(flush: true)) {
//            render(view: "edit", model: [IIUUOrdenamientoTerritorialInstance: IIUUOrdenamientoTerritorialInstance])
//            return
//        }
//
//        flash.message = message(code: 'default.updated.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), IIUUOrdenamientoTerritorialInstance.id])
//        redirect(action: "show", id: IIUUOrdenamientoTerritorialInstance.id)
//    }

    def delete(Long id) {
        def IIUUOrdenamientoTerritorialInstance = IIUUOrdenamientoTerritorial.get(id)
        if (!IIUUOrdenamientoTerritorialInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), id])
            redirect(action: "list")
            return
        }

        try {
            IIUUOrdenamientoTerritorialInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'IIUUOrdenamientoTerritorial.label', default: 'IIUUOrdenamientoTerritorial'), id])
            redirect(action: "show", id: id)
        }
    }
	
	def saveAjax() {
		def rs
		def localidad = params.localidad
		//Me fijo si ya existe
		IIUUOrdenamientoTerritorial ordenamiento = IIUUOrdenamientoTerritorial.findByLocalidad(localidad)
		if (!ordenamiento) {
			def ordenamientoTerritorialInstance = new IIUUOrdenamientoTerritorial(params)
			ordenamiento = ordenamientoTerritorialInstance.save(failOnError:true, flush: true)
			rs = 'alta'
		} else {
			rs = 'existente'
		}
			
		def jsonData = [rs: rs, id: ordenamiento.id]
		render jsonData as JSON

	}
	
//	def updateAjax() {
//		def rs = 'modificacion'
//		def localidad = params.localidad
//		IIUUOrdenamientoTerritorial ordenamientoTerritorialInstance = IIUUOrdenamientoTerritorial.findByLocalidad(localidad)
//		ordenamientoTerritorialInstance.properties = params
//		ordenamientoTerritorialInstance.save(flush: true)
//		def jsonData = [rs: rs]
//		render jsonData as JSON
//	}
}
